package com.et114.modules.cust.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.et114.components.simpletable.DataList;
import com.et114.core.common.CONST;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.modules.commons.XLSExport;
import com.et114.modules.cust.BIZ;
import com.et114.modules.cust.dao.IMng_CustDao;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.cust.vo.CustInfoVo;
import com.et114.modules.cust.vo.LeadVo;
import com.et114.modules.products.commons.Const;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/**
 * 客户信息维护
 * 
 * @author zhiweip
 * @version 1.0
 * @created 09-十一月-2010 23:03:49
 */
public class Mng_CustServiceImp extends BaseServicesImpl implements
		IMng_CustService {
	
	private IMng_CustDao	mng_CustDao;
	
	public void finalize ( ) throws Throwable {
		
	}
	
	public void setMng_CustDao ( IMng_CustDao mng_CustDao ) {
		this.mng_CustDao = mng_CustDao;
	}
	
	/**
	 * 客户详细信息
	 * 
	 * @param cvo
	 */
	public CustBaseVo loadCust ( CustBaseVo vo ) {
		CustBaseVo lvo = mng_CustDao.loadCust ( vo );
		if ( lvo != null && ! "edit".equalsIgnoreCase ( vo.getOperType ( ) ) ) {
			String no = lvo.getSafeno ( );
			if ( no != null && no.length ( ) > 4 ) {
				no = "***-**-" + no.substring ( no.length ( ) - 4 );
				lvo.setSafeno ( no );
			}
			lvo.setOperType ( "lock" );
		}
		return lvo;
	}
	
	public LeadVo loadLeadCust ( LeadVo vo ) {
		LeadVo lvo = mng_CustDao.loadLeadCust ( vo );
		if ( vo.getOperType ( ) == null
				|| ! vo.getOperType ( ).equals ( "edit" ) ) {
			String no = lvo.getSafeno ( );
			if ( no != null && no.length ( ) > 4 ) {
				no = "***-**-" + no.substring ( no.length ( ) - 4 );
				lvo.setSafeno ( no );
			}
			lvo.setOperType ( "lock" );
		}
		return lvo;
	}
	
	public List query_PUB_LEADER_LOG(LeadVo vo){
		return mng_CustDao.query_PUB_LEADER_LOG ( vo );
	}
	
	public List loadCustMsgByType ( CustInfoVo vo ) {
		List < CustInfoVo > list = mng_CustDao.loadCustMsgByType ( vo );
		
		// 如果不是管理员
		if ( vo.getOperType ( ) == null
				|| ! vo.getOperType ( ).equals ( "edit" ) )
			for ( CustInfoVo info : list ) {
				if ( info.getDtype ( ).equals ( BIZ.CUST_DTYPE_CRET ) ) {
					String no = info.getBillno ( );
					if ( no != null && no.length ( ) > 4 ) {
						if(no.length ( )!=19){
							no = "****-****-****-"
								+ no.substring ( no.length ( ) - 3 );
						}else{
							no = "****-****-****-"
								+ no.substring ( no.length ( ) - 4 );
						}
						info.setBillno ( no );
					} else {
						info.setBillno ( "****" );
					}
					info.setOperType ( "lock" );
					
				}
				JSONArray comjson = JSONArray.fromObject ( info );
				String result = comjson.toString ( );
				// //System.out.println (result);
				info.setRemoveMark ( result );
			}
		return list;
	}
	
	public static void main ( String [ ] args ) {
		String no = "2@163.com";
		int at = no.indexOf ( "@" );
		if ( at != - 1 ) {
			String s = no.substring ( 0 , at );
			String s2 = no.substring ( at , no.length ( ) );
			int l = s.length ( );
			if ( l > 3 ) {
				String n = s.substring ( 0 , l - 2 );
				String n2 = s.substring ( l - 2 , l );
				// System.out.println (""+n+"****"+n2);
			} else {
				String n = s.substring ( 0 , l );
				String n2 = s.substring ( 0 , l );
				// System.out.println (""+n+"****"+n2);
			}
		}
	}
	
	public int deleteCustById ( CustBaseVo cust , String custids ) {
		int i = 0;
		String custid[] = custids.split ( ";" );
		for ( String s : custid ) {
			if ( s.trim ( ) == "" )
				continue;
			cust.setCustid ( new Integer ( s ) );
			
			logger.debug ( "######## custid :" + cust.getCustid ( ) );
			i += mng_CustDao.deleteCustById ( cust );
		}
		if ( i == custid.length )
			return 1;
		return 0;
	}
	
	/**
	 * 客户查询
	 * 
	 * @param cvo
	 * @throws Exception
	 */
	public DataList queryCustDataList ( CustBaseVo vo ) throws Exception {
		logger.debug ( "######## getCusttype :" + vo.getCusttype ( ) );
		DataList dlist = mng_CustDao.queryCustDataList ( vo );
		List < CustBaseVo > list = dlist.getNowlist ( );
		for ( CustBaseVo lvo : list ) {
			String no = lvo.getSafeno ( );
			if ( no != null && no.length ( ) > 4 ) {
				no = "***-**-" + no.substring ( no.length ( ) - 4 );
				lvo.setSafeno ( no );
			}
		}
		return dlist;
	}
	
	public Integer newEmptyCust ( CustBaseVo vo ) {
		vo.setCusttype ( BIZ.CUST_LEAD );
		vo.setCustlever ( "1" );// 1普通,2优秀
		vo.setComerrom ( BIZ.CUST_SRC_REG );
		vo.setCustflag ( "0" );// 0,正常,1 删除
		
		Object obj = mng_CustDao.insertCustBaseInfo ( vo );
		
		return new Integer ( String.valueOf ( obj ) );
	}
	
	public Integer newCust ( CustBaseVo vo , HttpServletRequest request )
			throws Exception {
		Integer custid = null;
		CustInfoVo custinf;

		logger.debug ( " #######:" + vo.getCustid ( ) );
		if ( vo.getCustid ( ) != null ) {
			
			CustBaseVo custbasevo = new CustBaseVo ( );
			custbasevo.setCustid ( vo.getCustid ( ) );
			custbasevo = mng_CustDao.loadCust ( custbasevo );
			if ( custbasevo == null )
				throw new Exception ( "无数据！" );
			// 原成交客户,改为潜在客户 重置意向;
			if ( ! custbasevo.getCusttype ( ).equals ( "0" )
					&& vo.getCusttype ( ).equals ( "0" ) ) {
				vo.setLeaderid ( new Integer ( 99999999 ) );
			}
			custbasevo = null;
			
			custid = vo.getCustid ( );
			custinf = new CustInfoVo ( );
			custinf.setCustid ( custid );
			vo.setCustflag ( "0" );
			mng_CustDao.update_pub_cust_Byid_canSetNull ( vo );
			// mng_CustDao.deleteCustmsg ( custinf );
		} else {
			//vo.setCusttype ( BIZ.CUST_LEAD );
			vo.setCustlever ( "1" );
			vo.setComerrom ( BIZ.CUST_SRC_TEL );
			vo.setCustflag ( "0" );
			
			Object obj = mng_CustDao.insertCustBaseInfo ( vo );
			
			custid = new Integer ( String.valueOf ( obj ) );
		}
		custinf = null;
		//if(1==1) throw new Exception("");
		String linkinfo[] = request.getParameterValues ( "link_com" );
		if ( linkinfo != null ) {
			String link_tel[] = request.getParameterValues ( "link_tel" );
			String link_tax[] = request.getParameterValues ( "link_tax" );
			String link_addr[] = request.getParameterValues ( "link_addr" );
			String link_city[] = request.getParameterValues ( "link_city" );
			String link_state[] = request.getParameterValues ( "link_state" );
			String link_zip[] = request.getParameterValues ( "link_zip" );
			String link_lid[] = request.getParameterValues ( "link_lid" );
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_LINK );
			custinf.setCustid ( custid );
			custinf.setKeyids ( link_lid );
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
			
			for ( int i = 0 ; i < linkinfo.length ; i ++ ) {
				custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_LINK );
				custinf.setCustid ( custid );
				custinf.setComname ( linkinfo [ i ] );
				custinf.setBankno ( link_tax [ i ] );
				custinf.setBillcode ( link_tel [ i ] );
				custinf.setAddress ( link_addr [ i ] );
				custinf.setState ( link_state [ i ] );
				custinf.setCity ( link_city [ i ] );
				custinf.setZipcode ( link_zip [ i ] );
				if ( str ( link_lid [ i ] ) ) {
					custinf.setLid ( new Integer ( link_lid [ i ] ) );
					mng_CustDao.update_pub_cust_info ( custinf );
				} else
					mng_CustDao.insertCustMsg ( custinf );
				
				custinf = null;
			}
		}else{
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_LINK );
			custinf.setCustid ( custid ); 
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
		} 
		String lic_no[] = request.getParameterValues ( "lic_no" );
		if ( lic_no != null ) {
			String lic_level[] = request.getParameterValues ( "lic_level" );
			String lic_expire[] = request.getParameterValues ( "lic_expire" );
			String lic_addr[] = request.getParameterValues ( "lic_addr" );
			String lic_city[] = request.getParameterValues ( "lic_city" );
			String lic_state[] = request.getParameterValues ( "lic_state" );
			String lic_zip[] = request.getParameterValues ( "lic_zip" );
			String lic_lid[] = request.getParameterValues ( "lic_lid" );
			
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_LIC );
			custinf.setCustid ( custid );
			custinf.setKeyids ( lic_lid );
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
			
			for ( int i = 0 ; i < lic_no.length ; i ++ ) {
				custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_LIC );
				custinf.setCustid ( custid );
				custinf.setBillno ( lic_no [ i ] );
				custinf.setClevel ( lic_level [ i ] );
				custinf.setExpire ( lic_expire [ i ] );
				custinf.setAddress ( lic_addr [ i ] );
				custinf.setState ( lic_state [ i ] );
				custinf.setCity ( lic_city [ i ] );
				custinf.setZipcode ( lic_zip [ i ] );
				if ( str ( lic_lid [ i ] ) ) {
					custinf.setLid ( new Integer ( lic_lid [ i ] ) );
					mng_CustDao.update_pub_cust_info ( custinf );
				} else
					mng_CustDao.insertCustMsg ( custinf );
				custinf = null;
			}
		}else{
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_LIC );
			custinf.setCustid ( custid ); 
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
		}
		
		String cret_no[] = request.getParameterValues ( "cret_no" );
		if ( cret_no != null ) {
			String cret_expire[] = request.getParameterValues ( "cret_expire" );
			String cret_code[] = request.getParameterValues ( "cret_code" );
			String cret_ower[] = request.getParameterValues ( "cret_ower" );
			String cret_billaddr[] = request
					.getParameterValues ( "cret_billaddr" );
			String cret_city[] = request.getParameterValues ( "cret_city" );
			String cret_state[] = request.getParameterValues ( "cret_state" );
			String cret_zip[] = request.getParameterValues ( "cret_zip" );
			String cret_lid[] = request.getParameterValues ( "cret_lid" );
			
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_CERT );
			custinf.setCustid ( custid );
			custinf.setKeyids ( cret_lid );
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
			
			for ( int i = 0 ; i < cret_no.length ; i ++ ) {
				custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_CERT );
				custinf.setCustid ( custid );
				custinf.setBillcode ( cret_code [ i ] );
				custinf.setBillno ( cret_no [ i ] );
				custinf.setOwner ( cret_ower [ i ] );
				custinf.setExpire ( cret_expire [ i ] );
				custinf.setBilladd ( cret_billaddr [ i ] );
				custinf.setState ( cret_state [ i ] );
				custinf.setCity ( cret_city [ i ] );
				custinf.setZipcode ( cret_zip [ i ] );
				if ( str ( cret_lid [ i ] ) ) {
					custinf.setLid ( new Integer ( cret_lid [ i ] ) );
					mng_CustDao.update_pub_cust_info ( custinf );
				} else
					mng_CustDao.insertCustMsg ( custinf );
				
				custinf = null;
			}
			
		}else{
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_CERT );
			custinf.setCustid ( custid ); 
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
		}
		
		String bill_name[] = request.getParameterValues ( "bill_name" );
		if ( bill_name != null ) {
			String bill_bankno[] = request.getParameterValues ( "bill_bankno" );
			String bill_no[] = request.getParameterValues ( "bill_no" );
			String bill_ower[] = request.getParameterValues ( "bill_ower" );
			String bill_addr[] = request.getParameterValues ( "bill_addr" );
			String bill_city[] = request.getParameterValues ( "bill_city" );
			String bill_state[] = request.getParameterValues ( "bill_state" );
			String bill_zip[] = request.getParameterValues ( "bill_zip" );
			String bill_lid[] = request.getParameterValues ( "bill_lid" );
			String bill_code[] = request.getParameterValues ( "bill_code" );// 支票编号
			
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_BILL );
			custinf.setCustid ( custid );
			custinf.setKeyids ( bill_lid );
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
			
			for ( int i = 0 ; i < bill_name.length ; i ++ ) {
				custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_BILL );
				custinf.setCustid ( custid );
				custinf.setComname ( bill_name [ i ] );
				custinf.setBankno ( bill_bankno [ i ] );
				custinf.setBillno ( bill_no [ i ] );
				custinf.setOwner ( bill_ower [ i ] );
				custinf.setAddress ( bill_addr [ i ] );
				custinf.setState ( bill_state [ i ] );
				custinf.setCity ( bill_city [ i ] );
				custinf.setZipcode ( bill_zip [ i ] );
				custinf.setBillcode ( bill_code [ i ] );
				
				if ( str ( bill_lid [ i ] ) ) {
					custinf.setLid ( new Integer ( bill_lid [ i ] ) );
					mng_CustDao.update_pub_cust_info ( custinf );
				} else
					mng_CustDao.insertCustMsg ( custinf );
				
				custinf = null;
			}
		}else{
			custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_BILL );
			custinf.setCustid ( custid ); 
			mng_CustDao.deleteOtherCustmsg ( custinf );
			custinf = null;
		}
		
		return custid;
	}
	
	public void updateLeaderCustEmp ( final CustBaseVo vo , String [ ] custids ) {
		
		for ( String cid : custids ) {
			CustBaseVo dcust = new CustBaseVo ( );
			dcust.setCustid ( new Integer ( cid ) );
			dcust = mng_CustDao.loadCust ( dcust );
			if ( dcust == null )
				continue;
			
			CustBaseVo cust = new CustBaseVo ( );
			cust.setCustid ( dcust.getCustid ( ) );
			cust.setWorker ( vo.getWorker ( ) );
			cust.setUpdtUser ( vo.getUpdtUser ( ) );
			mng_CustDao.updateCustBaseInfo ( cust );
			cust = null;
			
			LeadVo lead = new LeadVo ( );
			lead.setLeaderid ( dcust.getLeaderid ( ) );
			lead.setCustid ( dcust.getCustid ( ) );
			lead.setLeademp ( vo.getWorker ( ) );
			mng_CustDao.update_PUB_LEADER ( lead );
			lead = null;
		}
		
	}
	
	public void deleteCustmsg ( CustInfoVo vo ) {
		mng_CustDao.deleteOtherCustmsg ( vo );
		
	}
	
	public void insertCustMsg ( CustInfoVo vo ) {
		mng_CustDao.insertCustMsg ( vo );
	}
	
	public Integer update_Lead_Intent ( LeadVo vo , HttpServletRequest request )
			throws Exception {
		CustBaseVo custbasevo = mng_CustDao.loadCust ( vo );
		if ( custbasevo == null )
			throw new Exception ( "无数据！" );
		LeadVo pvo = new LeadVo ( );
		pvo.setCustid ( vo.getCustid ( ) );
		pvo.setNextcalldate ( vo.getNextcalldate ( ) );
		pvo.setPreorderdate ( vo.getPreorderdate ( ) );
		pvo.setTradedate ( vo.getTradedate ( ) );
		pvo.setLrmk ( vo.getLrmk ( ) );
		pvo.setTradeflag ( "0" );
		pvo.setLeademp ( custbasevo.getWorker ( ) );
		pvo.setCrtUser ( vo.getCrtUser ( ) );
		// 主表
		Integer leaderid;
		if ( vo.getLeaderid ( ) != null ) {
			leaderid = vo.getLeaderid ( );
			pvo.setLeaderid ( leaderid );
			mng_CustDao.update_PUB_LEADER ( pvo );
		} else {
			leaderid = mng_CustDao.insert_PUB_LEADER ( pvo );
		}
		pvo.setLeaderid ( leaderid );
		mng_CustDao.insert_PUB_LEADER_LOG(pvo);
		
		pvo = null;
		
		pvo = new LeadVo ( );
		pvo.setCustid ( vo.getCustid ( ) );
		pvo.setLeaderid ( leaderid );
		pvo.setUpdtUser ( vo.getCrtUser ( ) );
		mng_CustDao.updateCustBaseInfo ( pvo );
		pvo = null;
		
		pvo = new LeadVo ( );
		pvo.setLeaderid ( leaderid );
		mng_CustDao.delete_PUB_LEADER_INTENT_ByLeaderid ( pvo );
		pvo = null;
		
		LeadVo lvo = null;
		String [ ] m_types = request.getParameterValues ( "m_types" );
		if ( m_types != null && m_types.length > 0 ) {
			String [ ] itemid = request.getParameterValues ( "m_itemid" );
			String [ ] biztype = request.getParameterValues ( "m_biztype" );
			String [ ] supname = request.getParameterValues ( "m_supname" );
			String [ ] itembrand = request.getParameterValues ( "m_itembrand" );
			String [ ] itemmodel = request.getParameterValues ( "m_itemmodel" );
			String [ ] itemkind = request.getParameterValues ( "m_itemkind" );
			String [ ] itemtitle = request.getParameterValues ( "m_itemtitle" );
			String [ ] serpid = request.getParameterValues ( "m_serpid" );
			String [ ] serplan = request.getParameterValues ( "m_serplan" );
			String [ ] rmk = request.getParameterValues ( "m_rmk" );
			for ( int i = 0 ; i < m_types.length ; i ++ ) {
				lvo = new LeadVo ( );
				lvo.setItemid ( new Integer ( itemid [ i ] ) );
				lvo.setLeaderid ( leaderid );
				lvo.setCustid ( vo.getCustid ( ) );
				lvo.setCrtUser ( vo.getCrtUser ( ) );
				lvo.setItemtype ( BIZ.TYPES_TEL );
				lvo.setBiztype ( biztype [ i ] );
				lvo.setSupname ( supname [ i ] );
				lvo.setItembrand ( itembrand [ i ] );
				lvo.setItemmodel ( itemmodel [ i ] );
				lvo.setItemkind ( itemkind [ i ] );
				lvo.setItemtitle ( itemtitle [ i ] );
				lvo.setSerpid ( serpid [ i ] );
				if ( serplan [ i ] != null && serplan [ i ].length ( ) > 120 )
					lvo.setSerplan ( serplan [ i ].substring ( 0 , 120 ) );
				else
					lvo.setSerplan ( serplan [ i ] );
				if ( rmk [ i ] != null && rmk [ i ].length ( ) > 120 )
					lvo.setIrmk ( rmk [ i ].substring ( 0 , 120 ) );
				else
					lvo.setIrmk ( rmk [ i ] );
				mng_CustDao.insert_PUB_LEADER_INTENT ( lvo );
				
			}
			
		}
		String c_types[] = request.getParameterValues ( "c_types" );
		if ( c_types != null && c_types.length > 0 ) {
			String [ ] itemid = request.getParameterValues ( "c_itemid" );
			String [ ] supname = request.getParameterValues ( "c_supname" );
			String [ ] itemmodel = request.getParameterValues ( "c_itemmodel" );
			String [ ] itemkind = request.getParameterValues ( "c_itemkind" );
			String [ ] itemtitle = request.getParameterValues ( "c_itemtitle" );
			String [ ] rmk = request.getParameterValues ( "c_rmk" );
			for ( int i = 0 ; i < c_types.length ; i ++ ) {
				lvo = new LeadVo ( );
				lvo.setLeaderid ( leaderid );
				lvo.setCustid ( vo.getCustid ( ) );
				lvo.setCrtUser ( vo.getCrtUser ( ) );
				lvo.setItemtype ( BIZ.TYPES_CERT );
				lvo.setItemid ( new Integer ( itemid [ i ] ) );
				lvo.setSupname ( supname [ i ] );
				lvo.setItemmodel ( itemmodel [ i ] );
				lvo.setItemkind ( itemkind [ i ] );
				lvo.setItemtitle ( itemtitle [ i ] );
				if ( rmk [ i ] != null && rmk [ i ].length ( ) > 120 )
					lvo.setIrmk ( rmk [ i ].substring ( 0 , 120 ) );
				else
					lvo.setIrmk ( rmk [ i ] );
				mng_CustDao.insert_PUB_LEADER_INTENT ( lvo );
				
			}
		}
		String s_types[] = request.getParameterValues ( "s_types" );
		if ( s_types != null && s_types.length > 0 ) {
			String [ ] itemid = request.getParameterValues ( "s_itemid" );
			String [ ] supname = request.getParameterValues ( "s_supname" );
			String [ ] itemkind = request.getParameterValues ( "s_itemkind" );
			String [ ] itemtitle = request.getParameterValues ( "s_itemtitle" );
			String [ ] rmk = request.getParameterValues ( "s_rmk" );
			for ( int i = 0 ; i < s_types.length ; i ++ ) {
				lvo = new LeadVo ( );
				lvo.setLeaderid ( leaderid );
				lvo.setCustid ( vo.getCustid ( ) );
				lvo.setCrtUser ( vo.getCrtUser ( ) );
				lvo.setItemtype ( BIZ.TYPES_SAFE );
				lvo.setItemid ( new Integer ( itemid [ i ] ) );
				lvo.setSupname ( supname [ i ] );
				lvo.setItemkind ( itemkind [ i ] );
				lvo.setItemtitle ( itemtitle [ i ] );
				if ( rmk [ i ] != null && rmk [ i ].length ( ) > 120 )
					lvo.setIrmk ( rmk [ i ].substring ( 0 , 120 ) );
				else
					lvo.setIrmk ( rmk [ i ] );
				mng_CustDao.insert_PUB_LEADER_INTENT ( lvo );
				
			}
		}
		
		String t_types[] = request.getParameterValues ( "t_types" );
		if ( t_types != null && t_types.length > 0 ) {
			String [ ] itemid = request.getParameterValues ( "t_itemid" );
			String [ ] supname = request.getParameterValues ( "t_supname" );
			String [ ] itemkind = request.getParameterValues ( "t_itemkind" );
			String [ ] itemtitle = request.getParameterValues ( "t_itemtitle" );
			;
			String [ ] rmk = request.getParameterValues ( "t_rmk" );
			for ( int i = 0 ; i < t_types.length ; i ++ ) {
				lvo = new LeadVo ( );
				lvo.setLeaderid ( leaderid );
				lvo.setCustid ( vo.getCustid ( ) );
				lvo.setCrtUser ( vo.getCrtUser ( ) );
				lvo.setItemtype ( BIZ.TYPES_TV );
				lvo.setItemid ( new Integer ( itemid [ i ] ) );
				lvo.setSupname ( supname [ i ] );
				lvo.setItemkind ( itemkind [ i ] );
				lvo.setItemtitle ( itemtitle [ i ] );
				if ( rmk [ i ] != null && rmk [ i ].length ( ) > 120 )
					lvo.setIrmk ( rmk [ i ].substring ( 0 , 120 ) );
				else
					lvo.setIrmk ( rmk [ i ] );
				mng_CustDao.insert_PUB_LEADER_INTENT ( lvo );
				
			}
		}
		
		String p_types[] = request.getParameterValues ( "p_types" );
		if ( p_types != null && p_types.length > 0 ) {
			String [ ] itemid = request.getParameterValues ( "p_itemid" );
			String [ ] supname = request.getParameterValues ( "p_supname" );
			String [ ] itembrand = request.getParameterValues ( "p_itembrand" );
			String [ ] itemmodel = request.getParameterValues ( "p_itemmodel" );
			String [ ] itemkind = request.getParameterValues ( "p_itemkind" );
			String [ ] itemtitle = request.getParameterValues ( "p_itemtitle" );
			String [ ] rmk = request.getParameterValues ( "p_rmk" );
			String [ ] serpid = request.getParameterValues ( "p_serpid" );
			String [ ] serplan = request.getParameterValues ( "p_serplan" );
			for ( int i = 0 ; i < p_types.length ; i ++ ) {
				lvo = new LeadVo ( );
				lvo.setLeaderid ( leaderid );
				lvo.setCustid ( vo.getCustid ( ) );
				lvo.setCrtUser ( vo.getCrtUser ( ) );
				lvo.setItemtype ( BIZ.TYPES_PC );
				lvo.setItemid ( new Integer ( itemid [ i ] ) );
				lvo.setSupname ( supname [ i ] );
				lvo.setItembrand ( itembrand [ i ] );
				lvo.setItemmodel ( itemmodel [ i ] );
				lvo.setItemkind ( itemkind [ i ] );
				lvo.setItemtitle ( itemtitle [ i ] );
				lvo.setSerpid ( serpid [ i ] );
				
				if ( serplan [ i ] != null && serplan [ i ].length ( ) > 120 )
					lvo.setSerplan ( serplan [ i ].substring ( 0 , 120 ) );
				else
					lvo.setSerplan ( serplan [ i ] );
				
				if ( rmk [ i ] != null && rmk [ i ].length ( ) > 120 )
					lvo.setIrmk ( rmk [ i ].substring ( 0 , 120 ) );
				else
					lvo.setIrmk ( rmk [ i ] );
				
				mng_CustDao.insert_PUB_LEADER_INTENT ( lvo );
				
			}
		}
		
		Integer result = null;
		
		return result;
	}
	
	public LeadVo loadLeader ( LeadVo vo ) {
		return mng_CustDao.load_PUB_LEADER_ByKey ( vo );
	}
	
	public List queryLeaderIntentList ( LeadVo vo ) {
		return mng_CustDao.queryLeaderIntentList ( vo );
	}
	
	/**
	 * 潜客查询
	 * 
	 * @throws Exception
	 */
	public DataList queryLeadDataList ( LeadVo vo ) throws Exception {
		DataList dlist = mng_CustDao.queryLeadDataList ( vo );
		List < CustBaseVo > list = dlist.getNowlist ( );
		for ( CustBaseVo lvo : list ) {
			String no = lvo.getSafeno ( );
			if ( no != null && no.length ( ) > 4 ) {
				no = "***-**-" + no.substring ( no.length ( ) - 4 );
				lvo.setSafeno ( no );
			}
		}
		return dlist;
	}
	
	public List queryCustInfoList ( CustInfoVo vo ) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List getItemTypes ( ) throws Exception {
		DictionaryCache bean = DictionaryCache.getInstance ( );
		
		List < BizOption > opList = new ArrayList < BizOption > ( );
		List < BizOption > options = bean.listBizOptions (
				Const.DIC_PRO_TYPE_KEY , false , CONST.COUNTRY_ZH );
		for ( BizOption option : options ) {
			BizOption op = new BizOption ( );
			op.setOptionName ( option.getOptionName ( ) );
			
			int t = Integer.parseInt ( option.getOptionValue ( ) );
			switch ( t ) {
				case 1 :
					op.setOptionValue ( BIZ.TYPES_TEL );
					break;
				case 2 :
					op.setOptionValue ( BIZ.TYPES_TV );
					break;
				case 3 :
					op.setOptionValue ( BIZ.TYPES_SAFE );
					break;
				case 4 :
					op.setOptionValue ( BIZ.TYPES_CERT );
					break;
				case 5 :
					op.setOptionValue ( BIZ.TYPES_PC );
					break;
				default :
					break;
			}
			opList.add ( op );
		}
		return opList;
	}
	
	public void toExpXls ( LeadVo vo , HttpServletResponse response )
			throws Exception {
		vo.setPageNum ( 0 );
		vo.setPageSize ( 5000 );
		List < LeadVo > list = mng_CustDao.queryLeadDataList ( vo )
				.getNowlist ( );
		
		XLSExport e = new XLSExport ( "Exp_leaders_"+System.currentTimeMillis ( )+".xls" );
		
		e.createRow ( 0 );
		e.setCell ( 0 , " 手机号码 ","b"  );
		e.setCell ( 1 , " 社会安全号 ","b"  );
		e.setCell ( 2 , " 客户姓 ","b"  );
		e.setCell ( 3 , " 客户名 ","b"  );
		e.setCell ( 4 , " 客户关爱专员 ","b"  );
		e.setCell ( 5 , " 下次回访日期 ","b"  );// cust.SEX, cust.BIRTH, cust.CITY,
		// cust.STATE, cust.ADDRESS, cust.RMK
		e.setCell ( 6 , " 性别 ","b"  );
		e.setCell ( 7 , " 生日 ","b"  );
		e.setCell ( 8 , " 所在州 ","b"  );
		e.setCell ( 9 , " 所在城市 ","b"  );
		e.setCell ( 10 , " 联系地址 ","b"  );
		e.setCell ( 11 , " 其它联系号码 " ,"b" );
		e.setCell ( 12 , " 电子邮箱 ","b"  );
		int i = 1;
		for ( LeadVo v : list ) {
			e.createRow ( i ++ );
			e.setCell ( 0 , v.getMobileno ( ) );
			e.setCell ( 1 , v.getSafeno ( ) );
			e.setCell ( 2 , v.getFamname ( ) );
			e.setCell ( 3 , v.getCustname ( ) );
			e.setCell ( 4 , v.getWorker ( ) );
			e.setCell ( 5 , v.getNextcalldate ( ) );
			e.setCell ( 6 , v.getSex ( ) );
			e.setCell ( 7 , v.getBirth ( ) );
			e.setCell ( 8 , v.getState ( ) );
			e.setCell ( 9 , v.getCity ( ) );
			e.setCell ( 10 , v.getAddress ( ) );
			e.setCell ( 11 , v.getOthertel ( ) );
			e.setCell ( 12 , v.getEmail ( ) );
		}
		try {
			e.exportXLS ( response );
			// e.exportXLS();
			// System.out.println( " 导出Excel文件[成功] " );
		} catch ( Exception e1 ) {
			// System.out.println( " 导出Excel文件[失败] " );
			e1.printStackTrace ( );
		}
		
	}
	
	public void toExpXls_Cust(CustBaseVo vo, HttpServletResponse response) throws Exception{
		vo.setPageNum ( 0 );
		vo.setPageSize ( 5000 );
		List<HashMap> list= mng_CustDao.queryCustDownDataList ( vo );
		
		 XLSExport e  =   new  XLSExport("Exp_Customer_"+System.currentTimeMillis ( )+".xls");
		 int i=0;
		 e.createRow( 0 );
		 e.setCell ( 0 , "下载规则："  ,"c");
		 e.createRow( 1 );
		 e.setCell ( 1 , "以客户信息为主，检索客户已购商品信息，并按订单状态，商品类型分类统计已购商品数量；" ,"c");
		 e.createRow( 2 );
		 e.setCell ( 1 , "所以同一客户,在不同订单状态和商品类型下会有多条记录；" ,"c");
		 e.setCell ( 1 , "积分为当前客户的总积分;非单个商品积分" ,"c");
		 
		 i=4; 
	        e.createRow( i++ );
	        e.setCell( 0 ,  " 手机/客户号码 ","b");
	        e.setCell( 1 ,  " 社会安全号 ","b" );
	        e.setCell( 2 ,  " 客户名 ","b" );
	        e.setCell( 3 ,  " 客户姓 ","b"  );
	        e.setCell( 4 ,  " 客户关爱专员 ","b"  ); 
	        e.setCell( 5 ,  " 客户类型 ","b"  );
	        e.setCell ( 6 , " 性别 ","b"  );
	        e.setCell ( 7 , " 生日 ","b"  );
	        e.setCell ( 8 , " 联系地址 ","b"  );
	        e.setCell ( 9 , " 所在城市 ","b"  );
	        e.setCell ( 10 , " 所在州 ","b"  );
	        e.setCell ( 11 , " 其它联系号码 " ,"b" );
	        e.setCell ( 12 , " 电子邮箱 " ,"b" );
	        e.setCell ( 13 , " 商品类型 " ,"b" );
	        e.setCell ( 14 , " 状态 ","b"  );
	        e.setCell ( 15 , " 合计数量 " ,"b" );
	        e.setCell ( 16 , " 当前积分 " ,"b" );
	        
	        for(Map v:list){
		        e.createRow( i++ );
		        
		        e.setCell ( 0 ,  toStr(v.get ("MOBILENO" )) );
		        e.setCell ( 1 ,  toStr(v.get ("SAFENO" )));
		        e.setCell ( 2 ,  toStr(v.get ("CUSTNAME" )) );
		        e.setCell ( 3 ,  toStr(v.get ("FAMNAME" )) );
		        e.setCell ( 4 ,  toStr(v.get ("WORKER" )) );
		        e.setCell ( 5 ,  toStr(v.get ("CUSTTYPE" )).equals ( BIZ.CUST_TRADE )?"成交客户":"潜在客户" ); 
		        e.setCell ( 6 ,  toStr(v.get ("SEX" )) );
		        e.setCell ( 7 ,  toStr(v.get ("BIRTH" )) );
		        e.setCell ( 8 ,  toStr(v.get ("ADDRESS" )) );
		        e.setCell ( 9 ,  toStr(v.get ("CITY" )) );
		        e.setCell ( 10 , toStr(v.get ("STATE" )) );
		        e.setCell ( 11 , toStr(v.get ("OTHERTEL" )) );
		        e.setCell ( 12 , toStr(v.get ("EMAIL" )) );
		        e.setCell ( 13 , toStr(v.get ("ITEMTYPE" )) );
		        String flag=toStr(v.get ("ORDERSTATE" ));
		        if("".equals ( flag) ){
		        	flag="";
		        }else{
		        	flag=flag.equals ( BIZ.ORDER_OK )?"正常":"作废";
		        }
		        e.setCell ( 14 , flag);
		        e.setCell ( 15 , toStr(v.get ("BUYNUM" )) );
		        e.setCell ( 16 , toStr(v.get ("INTEGRAL" )) );
	        }
	        
	        
	       
	         try    {
	            e.exportXLS(response);
	           // e.exportXLS();
	          // System.out.println( " 导出Excel文件[成功] " );
	        }   catch  (Exception e1)   {
	            //System.out.println( " 导出Excel文件[失败] " );
	            e1.printStackTrace();
	        }
	}
	
	public void toExpOrdXls_Cust(CustBaseVo vo, HttpServletResponse response) throws Exception{
		vo.setPageNum ( 0 );
		vo.setPageSize ( 5000 );
		List<HashMap> list= mng_CustDao.query_custDownDataList_by_order ( vo );
		
		 XLSExport e  =   new  XLSExport("Exp_Customer_"+System.currentTimeMillis ( )+".xls");
		 int i=0;
		 e.createRow( 0 );
		 e.setCell ( 0 , "下载规则："  ,"c");
		 e.createRow( 1 );
		 e.setCell ( 1 , "以客户信息为主，检索客户已购商品信息，并按订单状态，商品类型分类统计已购商品数量；积分为当前客户的总积分非单个商品积分" ,"c");
		 e.createRow( 2 );
		 e.setCell ( 1 , "所以同一客户,在不同订单状态和商品类型下会有多条记录；" ,"c");
		 e.createRow( 3 );
		 e.setCell ( 1 , "积分为当前客户的总积分;非单个商品积分" ,"c");
		 
		 i=4; 
	        e.createRow( i++ );
	        e.setCell( 0 ,  " 手机/客户号码 ","b");
	        e.setCell( 1 ,  " 社会安全号 ","b" );
	        e.setCell( 2 ,  " 客户名 ","b" );
	        e.setCell( 3 ,  " 客户姓 ","b"  );
	        e.setCell( 4 ,  " 客户关爱专员 ","b"  ); 
	        e.setCell( 5 ,  " 客户类型 ","b"  );
	        e.setCell ( 6 , " 性别 ","b"  );
	        e.setCell ( 7 , " 生日 ","b"  );
	        e.setCell ( 8 , " 联系地址 ","b"  );
	        e.setCell ( 9 , " 所在城市 ","b"  );
	        e.setCell ( 10 , " 所在州 ","b"  );
	        e.setCell ( 11 , " 其它联系号码 " ,"b" );
	        e.setCell ( 12 , " 电子邮箱 " ,"b" );
	        e.setCell ( 13 , " 商品类型 " ,"b" );
	        e.setCell ( 14 , " 订单日期 ","b"  ); 
	        e.setCell ( 15 , " 当前积分 " ,"b" );
	        for(Map v:list){
		        e.createRow( i++ );
		        
		        e.setCell ( 0 ,  toStr(v.get ("MOBILENO" )) );
		        e.setCell ( 1 ,  toStr(v.get ("SAFENO" )));
		        e.setCell ( 2 ,  toStr(v.get ("CUSTNAME" )) );
		        e.setCell ( 3 ,  toStr(v.get ("FAMNAME" )) );
		        e.setCell ( 4 ,  toStr(v.get ("WORKER" )) );
		        e.setCell ( 5 ,  toStr(v.get ("CUSTTYPE" )).equals ( BIZ.CUST_TRADE )?"成交客户":"潜在客户" ); 
		        e.setCell ( 6 ,  toStr(v.get ("SEX" )) );
		        e.setCell ( 7 ,  toStr(v.get ("BIRTH" )) );
		        e.setCell ( 8 ,  toStr(v.get ("ADDRESS" )) );
		        e.setCell ( 9 ,  toStr(v.get ("CITY" )) );
		        e.setCell ( 10 , toStr(v.get ("STATE" )) );
		        e.setCell ( 11 , toStr(v.get ("OTHERTEL" )) );
		        e.setCell ( 12 , toStr(v.get ("EMAIL" )) );
		        e.setCell ( 13 , toStr(v.get ("ITEMTYPE" )) );
		        e.setCell ( 14 , toStr(v.get ("ORDERDATE" ))); 
		        e.setCell ( 15 , toStr(v.get ("INTEGRAL" )) );
	        }
	        
	        
	       
	         try    {
	            e.exportXLS(response);
	           // e.exportXLS();
	           //System.out.println( " 导出Excel文件[成功] " );
	        }   catch  (Exception e1)   {
	           // System.out.println( " 导出Excel文件[失败] " );
	            e1.printStackTrace();
	        }
	}
	public DataList query_custOrdDataList ( CustBaseVo vo ) throws Exception{
		return mng_CustDao.query_custOrdDataList ( vo );
	}
	 public int deleteCust_info_byKey ( CustInfoVo vo ) {
		return mng_CustDao.deleteCust_info_byKey ( vo );
	}
	
	private boolean str ( Object o ) {
		if ( o == null || o.toString ( ).length ( ) < 1 )
			return false;
		else
			return true;
	}
	
	private String toStr ( Object o ) {
		if ( o == null || o.toString ( ).length ( ) < 1 )
			return "";
		else
			return String.valueOf ( o );
	}

	public int update_Pub_Cust_By_orderstate ( CustBaseVo vo ) {
		return mng_CustDao.update_Pub_Cust_By_orderstate(vo);
	}
}